#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
        unordered_map<int, vector<int>> link;
        vector<int> dp(n);
        vector<int> res;

        for (int i = 1; i < n; i++)
        {
            link[i].push_back(i - 1);
            dp[i] = i;
        }

        for (auto vec : queries)
        {
            link[vec[1]].push_back(vec[0]);
            for (int cur = vec[1]; cur < n; cur++)
            {
                for (auto prev : link[cur])
                {
                    dp[cur] = min(dp[cur], dp[prev] + 1);
                }
            }
            res.push_back(dp[n - 1]);
        }
        return res;
    }
};